package com.wealth.payment.web.controller;

import java.util.Date;

import javax.validation.Valid;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.wealth.payment.entity.PaymentAccount;
import com.wealth.payment.enums.PaymentTypeEnum;
import com.wealth.payment.service.IPaymentAccountService;
import com.wealth.system.service.ShiroUser;
import com.wealth.system.web.controller.BaseController;

import iaas.enums.StatusEnum;
import iaas.utils.RedirectMessage;

/** 
* @author wayne
*  
*/
@Controller
@RequestMapping(value="/manage/payment/account")
public class PaymentAccountController extends BaseController{
	
	@Autowired
	private IPaymentAccountService accountService;
	
	Logger logger = Logger.getLogger(PaymentAccountController.class);
	
	/**
	 * 所有RequestMapping方法调用前的Model准备方法, 实现Struts2 Preparable二次部分绑定的效果,先根据form的id从数据库查出Category对象,再把Form提交的内容绑定到该对象上。
	 * 因为仅update()方法的form中有id属性，因此仅在update时实际执行.
	 * 
	 */
	@ModelAttribute
	public void getCorpPayaccount(Model model, @RequestParam(value = "id", defaultValue = "-1") Long id) {
		try{
			if (id != -1) {
				PaymentAccount payaccount = accountService.get(id);
				model.addAttribute("payaccount", payaccount);
			}
			model.addAttribute("statusEnum", StatusEnum.values());
		}catch(Exception e){
			logger.error(" 领养分类ModelAttribute方法错误！", e);
		}
	}
	
	/**
	 * 新增|编辑微信支付账号 页面
	 */
	@RequestMapping(value="/wxpay", method = RequestMethod.GET)
	public String wxpay(Model model){
		try{
			Long corpid = ShiroUser.getInstance().getUser().getCorp().getId();
			PaymentAccount payaccount = accountService.findByCorpIdAndPaymode(corpid, PaymentTypeEnum.WECHAT.getValue());
			model.addAttribute("payaccount", payaccount);
			return "payment/account/wxpay";
		}catch(Exception e){
			logger.error(" 新增编辑微信支付账号 页面！", e);
			return "system/500";
		}
	}
	
	/** 
	* 新增|编辑微信支付账号 操作
	*/
	@RequestMapping(value="/updateWxpay",method = RequestMethod.POST)
	public String updateWxpay(@Valid @ModelAttribute("payaccount") PaymentAccount payaccount,
			RedirectAttributes redirectAttributes){
		try {
			
			if(payaccount.getId()==null){
				payaccount.setPaymode(PaymentTypeEnum.WECHAT.getValue());
				payaccount.setCorp(ShiroUser.getInstance().getUser().getCorp());
				payaccount.setCreator(ShiroUser.getInstance().getUser().getUsername());
				payaccount.setCreatetime(new Date());
				payaccount.setStatus(StatusEnum.NORMAL.getValue());
				accountService.save(payaccount);
				redirectAttributes.addFlashAttribute("message",  RedirectMessage.setShowTipSuccess("新增成功")  );
			}else{
				payaccount.setCreator(ShiroUser.getInstance().getUser().getUsername());
				payaccount.setCreatetime(new Date());
				accountService.save(payaccount);
				redirectAttributes.addFlashAttribute("message",  RedirectMessage.setShowTipSuccess("修改成功")  );
			}
			return "redirect:/manage/payment/account/wxpay";
		} catch (Exception e) {
			logger.error(" 新增错误", e);
			return "redirect:/manage/payment/account/wxpay";
		}
	}

	/**
	 * 新增|编辑支付宝支付账号 页面
	 */
	@RequestMapping(value="/alipay", method = RequestMethod.GET)
	public String alipay(Model model){
		try{
			Long corpid = ShiroUser.getInstance().getUser().getCorp().getId();
			PaymentAccount payaccount = accountService.findByCorpIdAndPaymode(corpid, PaymentTypeEnum.ALIPAY.getValue());
			model.addAttribute("payaccount", payaccount);
			
			return "payment/account/alipay";
		}catch(Exception e){
			logger.error(" 新增编辑微信支付账号 页面！", e);
			return "system/500";
		}
	}
	
	/** 
	* 新增|编辑支付宝支付账号操作
	*/
	@RequestMapping(value="/updateAlipay",method = RequestMethod.POST)
	public String updateAlipay(@Valid @ModelAttribute("payaccount") PaymentAccount payaccount,
			RedirectAttributes redirectAttributes){
		try {
			
			if(payaccount.getId()==null){
				payaccount.setPaymode(PaymentTypeEnum.ALIPAY.getValue());
				payaccount.setCorp(ShiroUser.getInstance().getUser().getCorp());
				payaccount.setCreator(ShiroUser.getInstance().getUser().getUsername());
				payaccount.setCreatetime(new Date());
				payaccount.setStatus(StatusEnum.NORMAL.getValue());
				accountService.save(payaccount);
				redirectAttributes.addFlashAttribute("message",  RedirectMessage.setShowTipSuccess("新增成功")  );
			}else{
				payaccount.setCreator(ShiroUser.getInstance().getUser().getUsername());
				payaccount.setCreatetime(new Date());
				accountService.save(payaccount);
				redirectAttributes.addFlashAttribute("message",  RedirectMessage.setShowTipSuccess("修改成功")  );
			}
			
			return "redirect:/manage/payment/account/alipay";
		} catch (Exception e) {
			logger.error(" 新增错误", e);
			return "redirect:/manage/payment/account/alipay";
		}
	}
	
}


